﻿Imports System.Security.Cryptography
Imports System.Windows.Forms
Imports System
Imports Microsoft.VisualBasic
Module Encryption

    Sub Encoding1(ByVal password As String, ByVal plainText As String)
        Dim wrapper As New Adv3Des(password)
        Dim cipherText As String = wrapper.EncryptData(plainText)
        My.Settings.text = cipherText
        My.Settings.Save()
    End Sub
    Sub Decode1(ByVal password As String, ByVal cipherText As String)
        Dim wrapper As New Adv3Des(password)
        ' DecryptData throws if the wrong password is used.
        Try
            Dim plainText As String = wrapper.DecryptData(cipherText)
            My.Settings.text = plainText
        Catch ex As System.Security.Cryptography.CryptographicException
        End Try
    End Sub

    ' Advanced Triple Des Encryption Class for Visual Basic
    ' Author: Todd Davies
    ' Web: http://www.cyberarmy.net
    ' Version: 1.0  -  14.11.2009

End Module

Public NotInheritable Class Adv3Des

    Private TripleDes As New TripleDESCryptoServiceProvider

    Private Function TruncateHash(ByVal keyBytes() As Byte, ByVal length As Integer) As Byte()
        Dim sha1 As New SHA1CryptoServiceProvider
        Dim hash() As Byte = sha1.ComputeHash(keyBytes)

        ReDim Preserve hash(length - 1)

        Return hash
    End Function

    Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte()
        Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key)

        Return TruncateHash(keyBytes, length)
    End Function

    Sub New(ByVal key As String)
        Dim saltBytes As Byte()
        Dim Gen As New RNGCryptoServiceProvider()

        saltBytes = New Byte(CType((TripleDes.BlockSize \ 8) - 1, Integer)) {}
        Gen.GetNonZeroBytes(saltBytes)
        TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
        TripleDes.IV = saltBytes
    End Sub

    Public Function EncryptData(ByVal plaintext As String) As String
        Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)
        Dim ms As New System.IO.MemoryStream
        Dim encStream As CryptoStream

        ms.Write(TripleDes.IV, 0, (TripleDes.BlockSize \ 8))

        encStream = New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
        encStream.FlushFinalBlock()

        Return Convert.ToBase64String(ms.ToArray)
    End Function

    Public Function DecryptData(ByVal encryptedtext As String) As String
        Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
        Dim ms As New System.IO.MemoryStream
        Dim decms As New System.IO.MemoryStream
        Dim decStream As CryptoStream
        Dim encText As Byte()
        Dim iv As Byte()
        Dim oldiv As Byte()

        ms.Write(encryptedBytes, 0, encryptedBytes.Length)
        encText = New Byte(CType(ms.Length - (TripleDes.BlockSize \ 8) - 1, Integer)) {}
        iv = New Byte(CType((TripleDes.BlockSize \ 8) - 1, Integer)) {}
        oldiv = New Byte(CType((TripleDes.BlockSize \ 8) - 1, Integer)) {}
        ms.Seek(0, IO.SeekOrigin.Begin)
        ms.Read(iv, 0, (TripleDes.BlockSize \ 8))
        ms.Read(encText, 0, encText.Length)

        oldiv = TripleDes.IV
        TripleDes.IV = iv

        decStream = New CryptoStream(decms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
        decStream.Write(encText, 0, encText.Length)
        decStream.FlushFinalBlock()

        TripleDes.IV = oldiv
        Return System.Text.Encoding.Unicode.GetString(decms.ToArray)
    End Function

End Class


'    Public NotInheritable Class Simple3Des
'        Private TripleDes As New TripleDESCryptoServiceProvider
'        Private Function TruncateHash( _
'            ByVal key As String, _
'            ByVal length As Integer) _
'            As Byte()

'            Dim sha1 As New SHA1CryptoServiceProvider

'            ' Hash the key.
'            Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key)
'            Dim hash() As Byte = sha1.ComputeHash(keyBytes)

'            ' Truncate or pad the hash.
'            ReDim Preserve hash(length - 1)
'            Return hash
'        End Function
'        Sub New(ByVal key As String)
'            ' Initialize the crypto provider.
'            key = "password"
'            TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
'            TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
'        End Sub
'        Public Function EncryptData( _
'            ByVal plaintext As String) _
'            As String

'            ' Convert the plaintext string to a byte array.
'            Dim plaintextBytes() As Byte = _
'                System.Text.Encoding.Unicode.GetBytes(plaintext)

'            ' Create the stream.
'            Dim ms As New System.IO.MemoryStream
'            ' Create the encoder to write to the stream.
'            Dim encStream As New CryptoStream(ms, _
'                TripleDes.CreateEncryptor(), _
'                System.Security.Cryptography.CryptoStreamMode.Write)

'            ' Use the crypto stream to write the byte array to the stream.
'            encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
'            encStream.FlushFinalBlock()

'            ' Convert the encrypted stream to a printable string.
'            Return Convert.ToBase64String(ms.ToArray)
'        End Function

'        Public Function DecryptData( _
'            ByVal encryptedtext As String) _
'            As String

'            ' Convert the encrypted text string to a byte array.
'            Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)

'            ' Create the stream.
'            Dim ms As New System.IO.MemoryStream
'            ' Create the decoder to write to the stream.
'            Dim decStream As New CryptoStream(ms, _
'                TripleDes.CreateDecryptor(), _
'                System.Security.Cryptography.CryptoStreamMode.Write)

'            ' Use the crypto stream to write the byte array to the stream.
'            decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
'            decStream.FlushFinalBlock()

'            ' Convert the plaintext stream to a string.
'            Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
'        End Function

'    End Class

'    'Private Sub MsgBox(ByVal p1 As String)
'    '    Throw New System.NotImplementedException
'    'End Sub

'End Module
